package com.baic.ibase.controller.api.v1;


import com.baic.ibase.models.dao.Products;
import com.baic.ibase.models.dao.Roles;
import com.baic.ibase.models.req.common.ApiResponse;
import com.baic.ibase.models.req.common.IdRequest;
import com.baic.ibase.models.req.common.PageRequest;
import com.baic.ibase.models.req.products.CreateProductReq;
import com.baic.ibase.models.req.products.DeleteProductReq;
import com.baic.ibase.service.products.ProductsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/api/v1/products")
public class ProductsController {

    @Autowired
    private ProductsService productsService;

    /**
     * @api {POST} /api/v1/products/ListProducts 查询产品列表
     * @apiVersion 1.0.0
     * @apiGroup ProductsController
     * @apiName listProducts()
     * @apiParam (请求体) {Number} pageNum
     * @apiParam (请求体) {Number} pageSize
     * @apiParam (请求体) {String} sort
     * @apiParam (请求体) {String} order
     * @apiParam (请求体) {String} requestId
     * @apiParamExample 请求体示例
     * {"requestId":"X8sr","pageSize":1445,"sort":"5","pageNum":4217,"order":"NH5yoau"}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Array} data.records
     * @apiSuccess (响应结果) {Number} data.records.id
     * @apiSuccess (响应结果) {String} data.records.productName 产品名称
     * @apiSuccess (响应结果) {String} data.records.productDescription 产品描述
     * @apiSuccess (响应结果) {String} data.records.productModels 产品物模型
     * @apiSuccess (响应结果) {Number} data.records.productStatus 产品状态
     * @apiSuccess (响应结果) {Number} data.records.productType 产品类型
     * @apiSuccess (响应结果) {Object} data.records.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.records.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.records.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.records.updatedUserId 更新人
     * @apiSuccess (响应结果) {Number} data.total
     * @apiSuccess (响应结果) {Number} data.size
     * @apiSuccess (响应结果) {Number} data.current
     * @apiSuccess (响应结果) {Array} data.orders
     * @apiSuccess (响应结果) {String} data.orders.column
     * @apiSuccess (响应结果) {Boolean} data.orders.asc
     * @apiSuccess (响应结果) {Boolean} data.optimizeCountSql
     * @apiSuccess (响应结果) {Boolean} data.searchCount
     * @apiSuccess (响应结果) {Boolean} data.optimizeJoinOfCountSql
     * @apiSuccess (响应结果) {Number} data.maxLimit
     * @apiSuccess (响应结果) {String} data.countId
     * @apiSuccessExample 响应结果示例
     * {"traceId":"osYW1","code":6632,"data":{"total":877,"current":7532,"size":2956,"optimizeCountSql":true,"records":[{"createdUserId":6348,"productStatus":369,"id":1571,"updatedUserId":1120,"productName":"fRKNaAEfu","productDescription":"ZlfuhLR2","productType":8680,"productModels":"0dO"}],"maxLimit":1713,"searchCount":true,"optimizeJoinOfCountSql":true,"orders":[{"asc":true,"column":"m16PE2dDNk"}],"countId":"zzuj8BsQ2"},"success":false,"message":"cV8ktJBtf","errors":"m0xagZ16V"}
     */
    @PostMapping("/ListProducts")
    public ApiResponse<Page<Products>> listProducts(@RequestBody PageRequest req) {
        Page<Products> page = productsService.page(new Page<>(req.getPageNum(), req.getPageSize()));
        return ApiResponse.ok(page);
    }

    /**
     * @api {POST} /api/v1/products/AddProducts 添加产品
     * @apiVersion 1.0.0
     * @apiGroup ProductsController
     * @apiName addProducts()
     * @apiParam (请求体) {String} productName 产品名称
     * @apiParam (请求体) {String} productDescription 产品描述
     * @apiParam (请求体) {String} productModels 产品模型
     * @apiParam (请求体) {Number} productType 产品类型
     * @apiParamExample 请求体示例
     * {"productName":"5VTN","productDescription":"F6QY8yTj2","productType":6885,"productModels":"4P5sh5"}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.productName 产品名称
     * @apiSuccess (响应结果) {String} data.productDescription 产品描述
     * @apiSuccess (响应结果) {String} data.productModels 产品物模型
     * @apiSuccess (响应结果) {Number} data.productStatus 产品状态
     * @apiSuccess (响应结果) {Number} data.productType 产品类型
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"bt","code":426,"data":{"createdUserId":9457,"productStatus":3673,"id":826,"updatedUserId":953,"productName":"M","productDescription":"j","productType":4115,"productModels":"eS9C7O6aNs"},"success":false,"message":"uBiv","errors":"ym12zOd"}
     */
    @PostMapping("/AddProducts")
    public ApiResponse<Products> addProducts(@RequestBody CreateProductReq req) {
        Products products = new Products();
        products.setProductName(req.getProductName());
        products.setProductDescription(req.getProductDescription());
        products.setProductModels(req.getProductModels());
        products.setCreatedTime(LocalDateTime.now());
        products.setUpdatedTime(LocalDateTime.now());
        products.setProductStatus(1);
        products.setProductType(req.getProductType());

        products.setCreatedUserId(1L);
        products.setUpdatedUserId(1L);

        if (productsService.save(products)) {
            log.info("addProducts products:{}", products);
            return ApiResponse.ok(products);
        } else {
            log.error("addProducts error");
            return ApiResponse.error(500, "添加产品失败");
        }
    }

    /**
     * @api {POST} /api/v1/products/DeleteProducts 删除产品
     * @apiVersion 1.0.0
     * @apiGroup ProductsController
     * @apiName deleteProducts()
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {String} requestId
     * @apiParamExample 请求体示例
     * {"requestId":"2tg6jR","id":1018}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Boolean} data
     * @apiSuccessExample 响应结果示例
     * {"traceId":"MCRt","code":9193,"data":false,"success":false,"message":"AYclGJvIUf","errors":"kcH"}
     */
    @PostMapping("/DeleteProducts")
    public ApiResponse<Boolean> deleteProducts(@RequestBody DeleteProductReq req) {
        if (productsService.removeById(req.getId())) {
            log.info("deleteProducts req:{}", req);
            return ApiResponse.ok(true);
        } else {
            log.error("deleteProducts error");
            return ApiResponse.error(500, "删除产品失败");
        }
    }

    /**
     * @api {POST} /api/v1/products/GetProducts 查询产品详情
     * @apiVersion 1.0.0
     * @apiGroup ProductsController
     * @apiName getProducts()
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {String} requestId
     * @apiParamExample 请求体示例
     * {"requestId":"WovK1Ss7M","id":9263}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.productName 产品名称
     * @apiSuccess (响应结果) {String} data.productDescription 产品描述
     * @apiSuccess (响应结果) {String} data.productModels 产品物模型
     * @apiSuccess (响应结果) {Number} data.productStatus 产品状态
     * @apiSuccess (响应结果) {Number} data.productType 产品类型
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"hWbgvQlED","code":732,"data":{"createdUserId":2506,"productStatus":5309,"id":6183,"updatedUserId":3983,"productName":"o34esFElX2","productDescription":"EyaS","productType":5759,"productModels":"Cd"},"success":false,"message":"12","errors":"DQL5"}
     */
    @PostMapping("/GetProducts")
    public ApiResponse<Products> getProducts(@RequestBody IdRequest req) {
        return ApiResponse.ok(productsService.getById(req.getId()));
    }

    /**
     * @api {POST} /api/v1/products/UpdateProducts 更新产品信息
     * @apiVersion 1.0.0
     * @apiGroup ProductsController
     * @apiName updateProducts()
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {String} productName 产品名称
     * @apiParam (请求体) {String} productDescription 产品描述
     * @apiParam (请求体) {String} productModels 产品物模型
     * @apiParam (请求体) {Number} productStatus 产品状态
     * @apiParam (请求体) {Number} productType 产品类型
     * @apiParam (请求体) {Object} createdTime 创建时间
     * @apiParam (请求体) {Object} updatedTime 更新时间
     * @apiParam (请求体) {Number} createdUserId 创建人
     * @apiParam (请求体) {Number} updatedUserId 更新人
     * @apiParamExample 请求体示例
     * {"createdUserId":7258,"productStatus":8571,"id":5561,"updatedUserId":443,"productName":"SRH22","productDescription":"ddhE","productType":5357,"productModels":"WqiDY"}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.productName 产品名称
     * @apiSuccess (响应结果) {String} data.productDescription 产品描述
     * @apiSuccess (响应结果) {String} data.productModels 产品物模型
     * @apiSuccess (响应结果) {Number} data.productStatus 产品状态
     * @apiSuccess (响应结果) {Number} data.productType 产品类型
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"uT","code":2616,"data":{"createdUserId":2296,"productStatus":6130,"id":3961,"updatedUserId":417,"productName":"gr2EgUPOxJ","productDescription":"7G2ECba2S","productType":4779,"productModels":"tPxAe3zz"},"success":true,"message":"L30t70","errors":"NY9sUuyv9"}
     */
    @PostMapping("/UpdateProducts")
    public ApiResponse<Products> updateProducts(@RequestBody Products req) {
        req.setUpdatedTime(LocalDateTime.now());
        if (productsService.updateById(req)) {
            log.info("updateProducts req:{}", req);
            return ApiResponse.ok(req);
        } else {
            log.error("updateProducts error");
            return ApiResponse.error(500, "更新产品失败");
        }
    }


}
